package com.hm.oper_monitor.init;

import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hm.common.util.ResourceUtil;
import com.hm.common.util.SpringUtil;
import com.hm.oper_monitor.bean.ResourcesManager;
import com.hm.oper_monitor.ui.initpanel.InitPage;
import com.hm.oper_monitor.ui.page.busi.BusiOperPage;
import com.hm.oper_monitor.ui.page.set.SetPage;
import com.hm.oper_monitor.ui.tab.TabPanel;
import com.hm.oper_monitor.ui.tab.TabPanelMnemonicEnum;

public class Init implements Runnable {
	private Logger logger = LoggerFactory.getLogger(getClass());
	private ResourcesManager rm;
	private JPanel backgroundPanel;
	private InitPage initPanel;
	

	public Init(JPanel backgroundPanel) {
		this.backgroundPanel = backgroundPanel;
		rm = SpringUtil.getBean(ResourcesManager.class);
	}

	public void run() {
		//删除历史叫号信息文件

		//初始化tab面板
		this.initTabPanel();
//		initLocalDB.init();
				
		Image setImage = ResourceUtil.getImage(this.getClass().getClassLoader(), "images/set.png");
		ImageIcon setIcon = new ImageIcon();
		setIcon.setImage(setImage);
		setIcon.setImage(setIcon.getImage().getScaledInstance(23, 23,  
	                Image.SCALE_DEFAULT)); 
		
		Image monitorImage = ResourceUtil.getImage(this.getClass().getClassLoader(), "images/monitor.png");
		ImageIcon monitorIcon = new ImageIcon();
		monitorIcon.setImage(monitorImage);
		monitorIcon.setImage(monitorIcon.getImage().getScaledInstance(23, 25,  
	                Image.SCALE_DEFAULT)); 
		//初始化set面板
		TabPanel.getTab().addPanel(SetPage.getPage().title,setIcon , SetPage.getPage(), SetPage.getPage().tip, TabPanelMnemonicEnum.set.getMnemonic());

		TabPanel.getTab().addPanel(BusiOperPage.getPage().title, monitorIcon, BusiOperPage.getPage(), BusiOperPage.getPage().tip, TabPanelMnemonicEnum.busi.getMnemonic());

		//显示初始化组件信息
//		WorkPanel.getPanel().gotoInitPage();//

		if (true){
			InitPage.getPage().addNewLabel("正在获取初始化配置信息");
			logger.info("正在获取初始化配置信息");
		}
		
		//获取消息服务器链接监听线程对象
		//如果连接模式是联机模式，则启动
		
		
		//获取初始化配置信息
		while(true){
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

		//设置该该机构是否有VIP窗口
		
		//启动设备初始化线程
		
		/*
		 * //监控初始化结果，初始化成功后启动取号界面
		while (true) {
			// TERMINATED 线程终止
			// 当初始化线程终止后停止循环
			if (State.TERMINATED == t.getState()) {
				logger.info("初始化线程终止");
				break;
			} else {
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		*/
		
		//初始化成功
		//如存在历史取号信息，则发送同步消息
//		logger.info("初始化成功");
		
		//跳转到欢迎页面
//		WorkPanel.getPanel().gotoFirstPage();

		//启动读卡器刷卡监听线程
		
		//启动呼叫器监听线程
		
		//启动评价器监听线程
	}

	private void initTabPanel() {
		TabPanel tabPanel = TabPanel.getTab();
		backgroundPanel.add(tabPanel, BorderLayout.CENTER);
		backgroundPanel.updateUI();
	}

	public JPanel getBackgroundPanel() {
		return backgroundPanel;
	}

	public void setBackgroundPanel(JPanel backgroundPanel) {
		this.backgroundPanel = backgroundPanel;
	}

	public InitPage getInitPanel() {
		return initPanel;
	}

	public void setInitPanel(InitPage initPanel) {
		this.initPanel = initPanel;
	}
}
